package com.lovo.common.frame;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.*;

import com.lovo.common.model.EmpBean;
import com.lovo.emp.service.IEmpService;
import com.lovo.emp.service.impl.EmpServiceImpl;
import com.lovo.netCRM.component.LovoImageLabel;
import com.lovo.netCRM.component.LovoPassWordTxt;
import com.lovo.netCRM.component.LovoTxt;
import com.lovo.system.service.ILogService;
import com.lovo.system.service.impl.LogServiceImpl;

/**
 * 
 * CRM管理系统
 * @description 登陆窗体
 */
public class LoginFrame extends JFrame{
	/**用户名文本框*/
	private LovoTxt userTxt = new LovoTxt("",260,240,this);
	/**密码文本框*/
	private LovoPassWordTxt pwdTxt = new LovoPassWordTxt("",260,300,this);
	private IEmpService empService = new EmpServiceImpl();
	private ILogService logService = new LogServiceImpl();
	public LoginFrame(){
		this.setLayout(null);
		LovoImageLabel userImg = new LovoImageLabel(280,220,50,50,this,"image/user.png");
		userImg.setBorder(null);
		
		LovoImageLabel pwdImg = new LovoImageLabel(280,280,50,50,this,"image/pwd.png");
		pwdImg.setBorder(null);
		
		LovoImageLabel enterImg = new LovoImageLabel(420,340,50,50,this);
		enterImg.setBorder(null);
		enterImg.setImage("image/login.png");

		enterImg.addMouseListener(new MouseAdapter() {

			@Override
			public void mouseClicked(MouseEvent arg0) {
				login();
			}
		});
		
		LovoImageLabel crmImg = new LovoImageLabel(0,0,840,570,this);
		crmImg.setImage("image/back.png");
		
		this.setSize(840,570);
		this.setVisible(true);
		this.setDefaultCloseOperation(3);
		this.setLocationRelativeTo(null);
	}
	
	//--------------------
	/**
	 * 登陆方法
	 */
	private void login(){
		//成功后,进入主界面

		EmpBean empBean = new EmpBean();
		empBean.setLoginName(userTxt.getText());
		empBean.setPwd(pwdTxt.getText());
		empBean = empService.login(empBean);
		if(empBean == null){
			JOptionPane.showMessageDialog(this,"登录名或密码错误，登录失败");
		}else{
			// 日志记录
			logService.add(empBean,"已登录");
			// 关闭当前窗口
			this.dispose();
			new MainFrame(empBean);
		}

	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		new LoginFrame();
	}

}
